/**
 * @ClassDescription:
 * @JdkVersion: 1.8
 * @Author: 李白
 * @Created: 2024/9/12 22:23
 */
import smile.data.DataFrame;
import smile.data.vector.DoubleVector;
import smile.data.vector.IntVector;
import smile.data.vector.StringVector;
import smile.classification.LogisticRegression;
import smile.validation.CrossValidation;
import smile.validation.metric.Accuracy;

public class LoanDefaultPrediction {
    public static void main(String[] args) {
        // Example data
        // Columns: [Feature1, Feature2, ..., FeatureN, Default]
        double[][] data = {
                {0.5, 1.2, 0.1, 1},
                {1.0, 0.8, 0.3, 0},
                {0.2, 1.0, 0.4, 1},
                // Add more data points as needed
        };

        // Convert data to DataFrame
        DataFrame df = DataFrame.of(
                DoubleVector.of("Feature1", data[0]),
                DoubleVector.of("Feature2", data[1]),
                DoubleVector.of("FeatureN", data[2]),
                IntVector.of("Default", data[3])
        );

        // Split into features and labels
        double[][] features = df.drop("Default").toArray();
        int[] labels = df.intVector("Default").toIntArray();

        // Create a Logistic Regression model
        LogisticRegression model = LogisticRegression.fit(features, labels);

        // Cross-validation
        CrossValidation cv = new CrossValidation(features.length, 5);
        double accuracy = 0;
        for (int[] trainIndex : cv.train) {
            double[][] trainData = smile.data.DataFrame.of(features).slice(trainIndex);
            int[] trainLabels = smile.data.DataFrame.of(labels).slice(trainIndex).toIntArray();

            double[][] testData = smile.data.DataFrame.of(features).slice(cv.test[0]);
            int[] testLabels = smile.data.DataFrame.of(labels).slice(cv.test[0]).toIntArray();

            LogisticRegression trainModel = LogisticRegression.fit(trainData, trainLabels);
            int[] predictions = trainModel.predict(testData);
            accuracy += Accuracy.of(testLabels, predictions);
        }
        accuracy /= cv.k;

        System.out.println("Cross-Validation Accuracy: " + accuracy);
    }
}
